using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.ComponentModel;

namespace C4F.DevKit.WebServices
{
    /// <summary>
    /// This class is base class for all components in the Coding4Fun Web services library.
    /// </summary>
    [ToolboxItem(false)]
    public class C4FWebServicesBase : Component
    {
        #region Private Members
        /// <summary>
        /// Network proxy that a request uses to access the Internet resource. 
        /// The request is made through the proxy server rather than directly to the Internet resource.
        /// </summary>
        private IWebProxy proxy;
        #endregion

        #region Public Properties
        /// <summary>
        /// Network proxy that a request uses to access the Internet resource. 
        /// The request is made through the proxy server rather than directly to the Internet resource.
        /// </summary>
        [Browsable(false)]
        public IWebProxy Proxy
        {
            get { return proxy; }
            set { proxy = value; }
        }

        #endregion

        #region Protected Functions/Methods
        /// <summary>
        /// Gets the response string for the specified requestUrl
        /// </summary>
        /// <param name="requestUrl">Request Url</param>
        /// <returns>Responsse data</returns>
        /// <exception cref="NotSupportedException">The request scheme specified in requestUrl has not been registered</exception>
        /// <exception cref="ArgumentNullException">requestUrl is a null reference</exception>
        /// <exception cref="UriFormatException">The URI specified in requestUrl is not a valid URI</exception>
        /// <exception cref="WebException">The time-out period for the request expired (OR) An error occurred while processing the request</exception>
        protected string GetQueryResponse(string requestUrl)
        {
            return GetQueryResponse(requestUrl, String.Empty, String.Empty);
        }

        /// <summary>
        /// Gets the response string for the specified requestUrl. Request is authenticated with the specified urlUserName and urlPassword
        /// </summary>
        /// <param name="requestUrl">Request Url</param>
        /// <param name="urlUserName">UserName for Request Url Authentication</param>
        /// <param name="urlPassword">Password for Request Url Authentication</param>
        /// <returns>Responsse data</returns>
        /// <exception cref="NotSupportedException">The request scheme specified in requestUrl has not been registered</exception>
        /// <exception cref="ArgumentNullException">requestUrl is a null reference</exception>
        /// <exception cref="UriFormatException">The URI specified in requestUrl is not a valid URI</exception>
        /// <exception cref="WebException">The time-out period for the request expired (OR) An error occurred while processing the request</exception>
        protected string GetQueryResponse(string requestUrl, string urlUserName, string urlPassword)
        {
            // Create a web request for input path.
            HttpWebRequest webRequest = WebRequest.Create(requestUrl) as HttpWebRequest;
            webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0;)";
            // If proxy settings have been specified, set the same.
            if (this.Proxy != null)
            {
                webRequest.Proxy = this.Proxy;
                webRequest.PreAuthenticate = true;
                // If url credentials have been specified, use the same.
                if (!string.IsNullOrEmpty(urlUserName))
                    webRequest.Credentials = new NetworkCredential(urlUserName, urlPassword);
            }
            // Get response data
            WebResponse webResponse = webRequest.GetResponse();
            StreamReader streamReader = new StreamReader(webResponse.GetResponseStream());
            string responseData = streamReader.ReadToEnd();
            streamReader.Close();

            return responseData;
        }
        #endregion
    }
}